<?php
namespace app\api\controller;
use app\api\model\Api as ApiModel;
/**
 * Description of Api
 *
 * @author 高磊<1455209597@qq.com>
 */
class Api extends \think\Controller{
    
    protected $result;
    protected $askData;
    
    function __construct() {
        parent::__construct();
        $this->askData= $this->request->param();     
    }
    #记录日志
    function __destruct() {
        $this->setLog($this->askData, $this->result);
    }
   /**
    * 校验器
    */
   protected function val($action){
       if(!isset(ApiModel::$valite[$action])){
           return false;
       }
       foreach (ApiModel::$valite[$action] as $k=>$v){
           if(!isset($this->askData[$v])||$this->askData[$v]==''){
               return false;
           }
       }
       return true;
   }
   /***
    * 保存日志记录（IO管理完全体）
    */
   public function setLog($IN,$OUT){
        $dirOut='./data/apilog';
        if(!is_dir($dirOut)){
            mkdir($dirOut);
        }
        $dirInt=$dirOut.'/'.date('Y-m');
        if(!is_dir($dirInt)){
            mkdir($dirInt);
        }
        $logcontent='********************************************    '. date('Y-m-d H:i:s')."\r\n";
        $logcontent.='url : '.$this->request->url()."\r\n";
        foreach($IN as $k =>$v){
            $logcontent.=$k.':'.$v."\r\n";
        }
        $logcontent.='-------------------------------------------------'."\r\n";
        foreach($OUT as $k =>$v){
            if(is_array($v)){
                $logcontent.=$k.':'.json_encode($v,256)."\r\n";
                continue;
            }
            $logcontent.=$k.':'.$v."\r\n";
        }  
        $logcontent.='********************************************    '."\r\n\r\n\r\n";
        $res= fopen($dirInt.'/'.date('d').'.txt','a+');
        fwrite($res, $logcontent, strlen($logcontent));       
        fclose($res);       
   }
    
}
